/**
 * 
 */
package com.kclmedu.forhelp.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

/**
 * 把数据导出到Excel中
 * @author yejf
 *
 */
public class ExportExcelUtil {

	private static final Logger LOGGER = Logger.getLogger(ExportExcelUtil.class);
	
	/*****************
	 * 导出数据到指定的Excel中,格式是 .xls
	 * @param datas 数据
	 * @param file 目标文件
	 * @return 文件名
	 */
	public static String export(List<List<String>> datas, String file) {
		String fileName = "";
		//1. 创建 WorkBook对象
		HSSFWorkbook wb = new HSSFWorkbook();
		//2. 创建一个Sheet
		HSSFSheet sheet = wb.createSheet("sheet1");//默认名字即可
		//2.1 为此sheet中的单元格设置默认的宽和高
		sheet.setDefaultColumnWidth(25 * 256);
		sheet.setDefaultRowHeight((short)(13.5*20));
		//3. 创建单元格样式
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HorizontalAlignment.LEFT); //左对齐

		//循环
		for(int i = 0; i < datas.size(); i++) {
			//4. 添加行
			HSSFRow row = sheet.createRow(i);
			//指定行高
			row.setHeight((short)(13.5*20));
			//创建列, 每行只有2列，固定的
			List<String> rowData = datas.get(i);
			for(int j = 0;j < rowData.size();j++) {
				//5.指定列宽
				sheet.setColumnWidth(j, 25*256);
				//创建第一列
				HSSFCell cell = row.createCell(j);
				//设置单元格格式
				cell.setCellStyle(style);
				//设置单元格类型
				cell.setCellType(CellType.STRING);
				//给单元格赋值
				cell.setCellValue(rowData.get(j));
			}
		}
		
		//7. 写出到目标文件
		File newFile = new File(file);
		if(newFile.isDirectory()) {
			//使用年月日时分秒来作为文件名
			String pattern = "yyyyMMdd-HHmmss";
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			String fn = sdf.format(new Date());
			newFile = new File(newFile, fn+".xls");
			//使用UUID生成唯一的文件名
//			newFile = new File(newFile, UUID.randomUUID().toString()+".xls");
		}
		//
		try {
			wb.write(newFile);
			LOGGER.debug("写入成功，路径是： "+newFile.getAbsolutePath());
			//记录文件名
			fileName = newFile.getName();
		} catch (IOException e) {
//			e.printStackTrace();
			LOGGER.error("写入目标Excel文件失败", e);
			throw new RuntimeException(e);
		} finally {
			//
			if(wb != null) {
				try {
					wb.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		//
//		System.out.println("over...");
		//
		return fileName;
	}
	
	/*public static void main(String[] args) {
		//
		String file = "d:\\temp\\test";
		//
		//模拟一些数据
		List<List<String>> mockDatas = new ArrayList<>();
		List<String> d1 = new ArrayList<>();
		d1.add("Model: JMC  JX1033");
		d1.add("Model: JMC  JX1033");
		List<String> d2 = new ArrayList<>();
		d2.add("Part No.: 9P2-7335-AA");
		d2.add("Part No.: 9P2-7335-AA");
		List<String> d3 = new ArrayList<>();
		d3.add("Part name:BUSH");
		d3.add("Part name:BUSH");
		
		//添加2空行
		List<String> gap = new ArrayList<>();
		gap.add(null);
		gap.add(null);
		
		List<String> d4 = new ArrayList<>();
		d4.add("Model: JMC  ");
		d4.add("Model: JMC  ");
		
		List<String> d5 = new ArrayList<>();
		d5.add("Part No.: 810400011");
		d5.add("Part No.: 810400011");
		
		List<String> d6 = new ArrayList<>();
		d6.add("Part name: COMPRESSOR");
		d6.add("Part name: COMPRESSOR");
		
		//
		mockDatas.add(d1);
		mockDatas.add(d2);
		mockDatas.add(d3);
		mockDatas.add(gap);
		mockDatas.add(gap);
		mockDatas.add(d4);
		mockDatas.add(d5);
		mockDatas.add(d6);
		
		export(mockDatas, file);
	}*/
}
